﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;


namespace SanGuo.Common
{
    public class XMLHTTP : IDisposable
    {
        #region 变量及参数
//        private MSXML2.XMLHTTPClass xml;
        
        private bool _alreadyDispose = false;

        public ST_Param Parameters;

        public enum SendMethod : int
        {
            POST, GET
        }
        #endregion

        #region 参数结构体
        public struct ST_Param
        {
            public string Url;
            public string Parameters;
            public string Uid;
            public string Pwd;
        }
        #endregion

        #region 发送数据
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="method">发送方式</param>
        /// <param name="p">数据</param>
        /// <returns>STRING</returns>
        public virtual string SendCommand(SendMethod method, ST_Param p)
        {
            if (p.Url == null || p.Url == "") return null;
            if (method == SendMethod.POST)
            {
                try
                {
                    xml.open("POST", p.Url, false, p.Uid, p.Pwd);
                    xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    xml.send(p.Parameters);
                    if (xml.status == 200)
                    {
                        //return System.Text.Encoding.GetEncoding("GB2312").GetString((byte[])xml.responseBody);
                        return System.Text.Encoding.GetEncoding("UTF-8").GetString((byte[])xml.responseBody);
                        //string b = System.Text.Encoding.GetEncoding("GB2312").GetString((byte[])xml.responseBody);
                        //return System.Text.Encoding.Default.GetString((byte[])xml.responseBody);
                    }
                    else
                    {
                        return xml.status.ToString();
                    }
                }
                catch (Exception E)
                {
                    return E.Message.ToString();
                }
            }
            else if (method == SendMethod.GET)
            {
                xml.open("GET", p.Url + "?" + p.Parameters, false, p.Uid, p.Pwd);
                xml.setRequestHeader("Content-type", "application/json");
                xml.send(null);
                return System.Text.Encoding.GetEncoding("UTF-8").GetString((byte[])xml.responseBody);
                //return System.Text.Encoding.Default.GetString((byte[])xml.responseBody);
            }
            return null;
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="method">发送方式</param>
        /// <param name="p">数据</param>
        /// <returns>STRING</returns>
        //public virtual object SendCommand(SendMethod method, ST_Param p, int n)
        //{
        //    if (p.Url == null || p.Url == "") return null;
        //    if (method == SendMethod.POST)
        //    {
        //        try
        //        {
        //            xml.open("POST", p.Url, false, p.Uid, p.Pwd);
        //            //xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        //            xml.setRequestHeader("Content-type", "text/xml");
        //            xml.send(p.Parameters);
        //            if (xml.status == 200)
        //            {
        //                //string a = System.Text.Encoding.GetEncoding("UTF-8").GetString((byte[])xml.responseBody);
        //                //return System.Text.Encoding.GetEncoding("UTF-8").GetString((byte[])xml.responseBody);
        //                //string b = System.Text.Encoding.GetEncoding("GB2312").GetString((byte[])xml.responseBody);

        //                return xml.responseXML;
        //            }
        //            else
        //            {
        //                return null;
        //            }
        //        }
        //        catch (Exception E)
        //        {
        //            return null;
        //        }
        //    }
        //    else if (method == SendMethod.GET)
        //    {
        //        xml.open("GET", p.Url + "?" + p.Parameters, false, p.Uid, p.Pwd);
        //        xml.send(null);
        //        return System.Text.Encoding.Default.GetString((byte[])xml.responseBody);
        //    }
        //    return null;
        //}

        #endregion

        #region 构造与释构
        public XMLHTTP()
        {
            xml = new MSXML2.XMLHTTPClass();
        }
        ~XMLHTTP()
        {
            Dispose();
        }
        protected virtual void Dispose(bool isDisposing)
        {
            if (_alreadyDispose) return;
            if (isDisposing)
            {
                if (xml != null)
                {
                    xml = null;
                }
            }
            _alreadyDispose = true;
        }
        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
